package com.five.monkey.util;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author 狻猊
 * @date 2021/8/9 下午12:10
 */
public class TimeUtils {

    private TimeUtils() {}

    private static final String FORMAT = "yyyy-MM-dd HH:mm:ss";

    public static String parseDate(Date date, String format) {
        return DateFormatUtils.format(date, format);
    }

    public static String parseDate(Date date) {
        return parseDate(date, FORMAT);
    }

    public static String parseDate() {
        return parseDate(new Date());
    }

    public static Date ofDate(String dateStr) {
        return ofDate(dateStr, FORMAT);
    }

    public static Date ofDate(String dateStr, String format) {
        Date date;
        if (StringUtils.isBlank(dateStr)) {
            date = new Date();
        } else {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
            try {
                date = simpleDateFormat.parse(dateStr);
            } catch (ParseException e) {
                date = new Date();
            }
        }
        return date;
    }
}
